
//getObj
function $(a_sSelect) {
    var aArgs=this.arguments;
    var sSelector = a_sSelector.replace(/\s*/g,""), sName;
    var oObj=null, a_oObj;

    if(aArgs.length>1) {a_oObj=aArgs[1];delete aArgs;}

    //getElementById
    if(sSelector.indexOf('#')==0) {
        sSelector = sSelector.substring(1,sSelector.length);

        if (document.getElementById) {
            oObj= document.getElementById(sSelector);
        } else if (document.all) {
            oObj = document.all[sSelector];
        } else if (document.layers) {
            oObj = document.layers[sSelector];
        }
    } else {//getElementByTag
        if(document.getElementsByTagName) {
            if(a_oObj) {
                oObj = a_oObj.getElementsByTagName(sSelector);
            } else {
                oObj = document.getElementsByTagName(sSelector);
            }
        }
    }

    return oObj;
}

/**
 * CJJ's JS Framework 
 *
 * @type : class
 * @author : [BI]CJJ http://www.imcjj.com
 * @date : 2007-06-01
 * @memo : this program is a business application
 */
function TJSFramework() {
    this.unloadModule = TJSFramework_unloadModule;
    this.eventListen = TJSFramework_eventListen;
    this.Error=null;
}

/**
 * unload extenal module
 *
 * @type : method
 * @parameters : a_sModuleNames, module name list split with ","
 * @return : none
 * @author : [BI]CJJ http://www.imcjj.com
 * @date : 2007-06-02
 * @memo :support by document.getElementsByTagName
 */
function TJSFramework_unloadModule(a_sModuleNames, a_sModulePath) {
    if(!document.getElementsByTagName) return;

    var oScripts=document.getElementsByTagName("script"), oScript=null, oHead = document.getElementsByTagName("head")[0];
    var sSrc, sJSFile, sModulePath = a_sModulePath.toLowerCase();
    var aModules = a_sModuleNames.toLowerCase().split(",");
    var i, j, iCount;

    for(i=0;i<aModules.length;i++) {
        sSrc=sModulePath + aModules[i];

        for(j=0;j<oScripts.length;j++) {
            if(oScripts[j].src.toLowerCase().indexOf(sSrc)>-1) {oHead.removeChild(oScripts[j]);break;}
        }
    }
}

/**
 * load extenal module
 *
 * @type : method
 * @parameters : a_sModuleNames, module name list split with ","
 * @return : none
 * @author : [BI]CJJ http://www.imcjj.com
 * @date : 2007-06-02
 * @memo :support by document.getElementsByTagName
 */
function _loadModule() {
    if(!document.getElementsByTagName) return;

    var oScripts=document.getElementsByTagName("script"), oScript=null, oJSFramework=null;
    var bHasLoaded = false, bGetFramework=false;
    var sSrc = "", sJSFile, sPath="";
    var aFiles=[], aParameters;
    var i, j, iCount, iPos=-1;

    for(j=0;j<oScripts.length;j++) {
        sJSFile = oScripts[j].src.toLowerCase();
        iPos=sJSFile.indexOf("tjsframework.js");

        if (iPos>-1) {
            sPath=sJSFile.substr(0,iPos);
            aParameters=sJSFile.split("?");
            if(aParameters.length==2) {
                oScripts[j].src=aParameters[0];
                aFiles=aParameters[1].split(",");
                break;
            }
        }
    }

    iCount=aFiles.length;

    if(iCount>0) {
        for(i=0;i<iCount;i++) {document.write('<script type="text/javascript" src="'+sPath+'TJSFramework.'+ aFiles[i] + '.js" language="javascript"></scrip'+'t>');}
    }
}

/**
 * add method of dynamic to the class 
 *
 * @type : method
 * @parameters : a_sModuleNames, module name list split with ","
 * @return : none
 * @author : [BI]CJJ http://www.imcjj.com
 * @date : 2007-06-02
 * @memo :support by document.getElementsByTagName
 */
Function.prototype.addModule = function (a_sName, a_fName) {
    var oClass=this;

    if (!a_sName || !oClass || "function"!=typeof oClass) return oClass;
    a_sName = String(a_sName); oClass = oClass.prototype;

    if (a_sName && "function"!=typeof oClass[a_sName]) {

        if (a_sName && a_fName && "function"==typeof a_fName)
            oClass[a_sName] = new Function("v","this." + a_sName + "=" + a_fName + ".apply(this,[v]);");
        else
            oClass[a_sName] = new Function("v", "this." + a_sName + "=v;");

        return oClass[a_sName];
    }

    return oClass;
};

//DOM.eventListen method
function TJSFramework_eventListen(a_oObj,a_sEventType) {
    //return eventListen object
    return new TEventListen(a_oObj,a_sEventType);
}

//event listen class
function TEventListen(a_oObj,a_sEventType) {
    this.Listeners=[];
    a_oObj.eventListen=this;
    a_oObj[a_sEventType] = TEventListen_callback;

    this.addListener = TEventListen_addListener;
    this.removeListener = TEventListen_removeListener;
    this.Notify=TEventListen_Notify;
    this.callback = TEventListen_callback;
}

//add event listen
function TEventListen_addListener(a_oObj) {
    var aListeners=this.Listeners;
    var iCount = aListeners.length;

    for (var i=0;i<iCount;i++){
        if (aListeners[i]==a_oObj){return;}
    }

    aListeners[iCount]=a_oObj;;
    this.Listeners = aListeners;
}

//remove event listen
function TEventListen_removeListener(a_oObj) {
    var aListeners = this.Listeners;
    var iCount = aListeners.length;

    for (var i=0;i<iCount;i++){
        if (aListeners[i]==a_oObj){
            aListeners.splice(i,1);
        }
    }

    this.Listeners = aListeners;
}

//event listen notify
function TEventListen_Notify(a_oEvent){
     var aList = this.Listeners;
     var oListener;

     for(var i=0;i<aList.length;i++) {
         oListener=aList[i];
         oListener.call(this,a_oEvent);
     }
}

//evnet listen callback
function TEventListen_callback(event) {
    var oEvent = event || window.event;
    var oListen = this.eventListen;
    oListen.Notify(oEvent);
}

_loadModule();